 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
 #include "crypto/crypt_eal_pkey.h"
 #include "bsl/bsl_sal.h"
 #include "bsl/bsl_err.h"
 #include "crypto/crypt_algid.h"
 #include "crypto/crypt_errno.h"
 #include "crypto/crypt_eal_rand.h"
 #include "crypto/crypt_eal_init.h"
 
 void PrintLastError(void) {
     const char *file = NULL;
     uint32_t line = 0;
     BSL_ERR_GetLastErrorFileLine(&file, &line);
     printf("failed at file %s at line %d\n", file, line);
 }
 
 int main(void)
 {
     int ret;
     uint8_t msg[32] = {0};             // 要签名的消息（这里填充 32 字节 0）
     uint8_t signBuf[300] = {0};        // RSA-2048 签名最大 ~256 字节，留足空间
     uint32_t signLen = sizeof(signBuf);
     CRYPT_EAL_PkeyCtx *ctx = NULL;
 
     // 1. 初始化库
     ret = CRYPT_EAL_Init(CRYPT_EAL_INIT_ALL);
     if (ret != CRYPT_SUCCESS) {
         printf("error code is %x\n", ret);
         goto EXIT;
     }
 
     // 2. 创建 RSA 上下文
     ctx = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_RSA);
     if (ctx == NULL) {
         goto EXIT;
     }
 
     // 3. 生成 RSA-2048 密钥对
     ret = CRYPT_EAL_PkeyGen(ctx);
     if (ret != CRYPT_SUCCESS) {
         printf("error code is %x\n", ret);
         PrintLastError();
         goto EXIT;
     }
 
     // 4. 使用 SHA-256 进行签名
     ret = CRYPT_EAL_PkeySign(ctx, CRYPT_MD_SHA256, msg, sizeof(msg), signBuf, &signLen);
     if (ret != CRYPT_SUCCESS) {
         printf("error code is %x\n", ret);
         PrintLastError();
         goto EXIT;
     }
 
     // 5. 验签
     ret = CRYPT_EAL_PkeyVerify(ctx, CRYPT_MD_SHA256, msg, sizeof(msg), signBuf, signLen);
     if (ret != CRYPT_SUCCESS) {
         printf("error code is %x\n", ret);
         PrintLastError();
         goto EXIT;
     }
 
     printf("pass \n");
 
 EXIT:
     // 6. 释放资源
     CRYPT_EAL_PkeyFreeCtx(ctx);
     CRYPT_EAL_RandDeinit();
     BSL_ERR_DeInit();
     return ret;
 }
 